package com.nit.bike.service.system.impl;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.basic.UserDTO;
import com.nit.bike.dto.system.AccountDTO;
import com.nit.bike.dto.system.PermissionDTO;
import com.nit.bike.dto.system.RoleDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.service.basic.UserService;
import com.nit.bike.service.system.AccountService;
import com.nit.bike.service.system.RoleService;
import com.nit.bike.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @Value(value = "${application.jwt.expire}")
    private Integer expire;

    @Override
    public JSONObject loginCheck(String loginName, String loginPwd) throws Exception {
        JSONObject json = new JSONObject();
        TokenDTO tokenDTO = new TokenDTO();
        OpResultDTO op = new OpResultDTO();
        json.put("data", op);
        AccountDTO accountDTO = new AccountDTO();
        UserDTO userDTO = userService.loginCheck(loginName);
        if (userDTO == null || !userDTO.getLoginPwd().equals(loginPwd)) {
            op.setResult("");
            json.put("message", "用户名或密码错误");
            json.put("status", 401);
        } else {
            if (userDTO.getUserState() == 1) {
                op.setResult("");
                json.put("message", "账户已被停用，请联系系统管理员");
                json.put("status", 401);
            } else {
                accountDTO.setId(userDTO.getUserId());
                accountDTO.setName(userDTO.getUserName());
                accountDTO.setRoleId(String.valueOf(userDTO.getRoleId()));
                // 生成签名DTO，包含账号ID，账户类型
                tokenDTO.setRoleId(userDTO.getRoleId());
                tokenDTO.setAccountId(userDTO.getUserId());
                accountDTO.setToken(JWTUtil.createSign(tokenDTO.toString(), expire));
                op.setResult(accountDTO);
                json.put("message", "");
                json.put("status", 200);
            }
        }
        return json;
    }

    @Override
    public AccountDTO getInfo(String token) throws Exception {
        AccountDTO accountDTO = new AccountDTO();
        TokenDTO tokenDTO = JWTUtil.verifyToken(token);
        Integer roleId = tokenDTO.getRoleId();

        UserDTO userDTO = userService.getByUserId(tokenDTO.getAccountId());
        accountDTO.setId(userDTO.getUserId());
        accountDTO.setUserName(userDTO.getUserName());
        accountDTO.setRoleId(String.valueOf(userDTO.getRoleId()));

        accountDTO.setToken(JWTUtil.createSign(tokenDTO.toString(), expire));
        List<PermissionDTO> listPermissionDto = roleService.listPermissionByRoleId(roleId);
        RoleDTO roleDTO = roleService.getRoleByRoleId(roleId);
        roleDTO.setPermissions(listPermissionDto);
        accountDTO.setRole(roleDTO);
        return accountDTO;
    }
}
